import { NextResponse } from "next/server"
import type { NextRequest } from 'next/server'
import {itemDeleteZodFormData,z} from "@/shared/formDataSchema"
import {deleteItemById} from "@/server/database/repositories/itemOperation"

export async function DELETE(req: NextRequest,
  {params}: {params: {id: string}}) {
  try {
    const id = + params.id
    itemDeleteZodFormData.parse({id})
    const count = await deleteItemById(id)
    if(count){
      return NextResponse.json({
        resource: count,
        msg: '已删除账目！'
      })
    }else{
      return NextResponse.json( '删除账目失败！', { status: 500} )
    }
  }
  catch (error) {
    if (error instanceof z.ZodError) {
      const statusMessage = error.issues.map(issue => issue.message).join("; ");
      return NextResponse.json( statusMessage, { status: 400} )
    }
    return NextResponse.json( '服务器出错，删除账目失败！', { status: 500} )
  }
}